package com.hoshiicloud.user.controller.organization;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.entity.organization.StoreOrganizationAttachment;
import com.hoshiicloud.user.param.organization.StoreOrganizationAttachmentAddParam;
import com.hoshiicloud.user.param.organization.StoreOrganizationAttachmentQueryParam;
import com.hoshiicloud.user.param.organization.StoreOrganizationAttachmentUpdateParam;
import com.hoshiicloud.user.service.organization.StoreOrganizationAttachmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 店铺供应商附件 控制器
 * </p>
 *
 * @author campchen
 * @since 2019-04-28
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/organization/store-organization-attachment")
@Api(value = "平台店铺机构附件", tags = {"平台店铺机构附件"})
@ApiSort(44)
public class StoreOrganizationAttachmentController {
    @Autowired
    private StoreOrganizationAttachmentService storeOrganizationAttachmentService;

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询店铺供应商附件
     */
    @ApiOperation(value = "分页查询店铺供应商附件")
    @PostMapping("/getPage")
    public Response page(@RequestBody StoreOrganizationAttachmentQueryParam param) {
        try {
            QueryWrapper<StoreOrganizationAttachment> queryWrapper = new QueryWrapper<StoreOrganizationAttachment>();
            queryWrapper.lambda()
                    .eq((param.getVendorId() != null && param.getVendorId() != 0), StoreOrganizationAttachment::getVendorId, param.getVendorId())
                    .eq(StringUtils.isNotBlank(param.getAttachName()), StoreOrganizationAttachment::getAttachName, param.getAttachName())
                    .eq(StringUtils.isNotBlank(param.getAttachUrl()), StoreOrganizationAttachment::getAttachUrl, param.getAttachUrl())
                    .eq(StringUtils.isNotBlank(param.getExpirationDate()), StoreOrganizationAttachment::getExpirationDate, param.getExpirationDate());
            Page<StoreOrganizationAttachment> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(storeOrganizationAttachmentService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询店铺供应商附件异常", e);
            return Response.failed("分页查询店铺供应商附件异常");
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询店铺供应商附件
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询店铺供应商附件")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(storeOrganizationAttachmentService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增店铺供应商附件")
    public Response save(@RequestBody @Valid StoreOrganizationAttachmentAddParam param) {
        StoreOrganizationAttachment data;
        try {
            data = BeanUtils.covertType(param, StoreOrganizationAttachment.class);
        } catch (Exception e) {
            log.error("新增店铺供应商附件Bean转换异常", e);
            return Response.failed("新增店铺供应商附件Bean转换异常");
        }
        return Response.data(storeOrganizationAttachmentService.save(data));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改店铺供应商附件
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改店铺供应商附件")
    public Response update(@RequestBody @Valid StoreOrganizationAttachmentUpdateParam param) {
        StoreOrganizationAttachment data;
        try {
            data = BeanUtils.covertType(param, StoreOrganizationAttachment.class);
        } catch (Exception e) {
            log.error("修改店铺供应商附件异常", e);
            return Response.failed("修改店铺供应商附件异常");
        }
        return Response.data(storeOrganizationAttachmentService.updateById(data));
    }

    @PostMapping("/insertAttach4StoreOrganization")
    @ApiOperation(value = "批量新增租户供应商附件store_organization_attachment")
    public Response insertAttach4StoreOrganization(@RequestBody @Valid List<StoreOrganizationAttachment> list){
        return Response.data(storeOrganizationAttachmentService.insertAttach4StoreOrganization(list));
    }
}
